<template>
	<view class="goods" @tap="goGoodDetail" @longtap="deleteGoods">
		<view class="image-box">
			<checkbox v-if='showCheck' :checked="goods.checked" color="#D9557C" @tap.stop="changechecked" />
			<view class="goods-left">
				<image :src="goods.goods_big_logo || defaultImg" mode="widthFix" class="goods-tp"></image>
			</view>
		</view>
		<view class="goods-info">
			<view class="goods-name">
				{{goods.goods_name}}
			</view>

			<view class="goods-price-box">
				<view class="goods-price">
					￥{{goods.goods_price}}
				</view>
				<view class="goods-num" v-if='vshowNum'>
					<view class="num-edit-sub" :class="goods.cartNum==1?'editColor':''" @tap.stop="numSub">-</view>
					<view class="num">{{goods.cartNum}}</view>
					<view class="num-edit-add" @tap.stop="numAdd">+</view>
				</view>
				<view class="num-only" v-if="vshowNumOnly" @tap.stop="showNumEdit">X{{goods.cartNum}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "ljy-goods",
		data() {
			return {
				defaultImg: '../../static/default_image.png',
				
				vshowNum: this.showNum,
				vshowNumOnly: this.showNumOnly
			};
		},
		// props:['goods'],
		props: {
			goods: {
				type: Object,
				default: null
			},
			showCheck: {
				type: Boolean,
				default: false
			},
			showNum: {
				type: Boolean,
				default: false
			},
			allowLongTap: {
				type: Boolean,
				default: false
			},
			showNumOnly: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			showNumEdit() {
				this.vshowNumOnly = false;
				this.vshowNum = true;
			},
			goGoodDetail() {
				uni.navigateTo({
					url: '../../subpkg1/goods-detail/goods-detail?good_id=' + this.goods.goods_id
				})
			},
			changechecked() {
				this.$emit('changechecked', {
					goods_id: this.goods.goods_id,
					checked:!this.goods.checked
				})
			},
			numSub() {
				if (this.goods.cartNum > 1) {
					this.$emit('changeNum', {
						goods_id: this.goods.goods_id,
						cartNum: this.goods.cartNum - 1,
					})
				}
			},
			numAdd() {
				this.$emit('changeNum', {
					goods_id: this.goods.goods_id,
					cartNum: this.goods.cartNum + 1,
				})
			},
			deleteGoods() {
				if(this.allowLongTap) {
					uni.showModal({
						title:'删除',
						content:'是否确认删除？',
						success:(res) => {
							console.log(res);
							if(res.confirm) {
								this.$emit('changeNum', {
									goods_id: this.goods.goods_id,
									checked:!this.goods.checked,
									cartNum: 0,
								})
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.goods {
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #cecece;
		
		.image-box {
			flex: 3;
			display: flex;
			justify-content: center;
			align-items: center;
			
			checkbox {
				color: #000;
			}
			
			.goods-left {
			
				.goods-tp {
					width: 225rpx;
					display: block;
				}
			}
		}

		

		.goods-info {
			padding: 0 10rpx;
			flex: 7;
			display: flex;
			flex-direction: column;
			justify-content: flex-start;

			.goods-name {
				flex: 1;
				font-size: 14px;
				font-weight: 500;
			}

			.goods-price-box {
				flex: 1;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.goods-price {
					font-size: 20px;
					font-weight: 500;
					color: red;

				}

				.goods-num {
					display: flex;

					.num-edit-sub {
						width: 55rpx;
						height: 55rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 1px solid #a5a5a5;
						border-top-left-radius: 50%;
						border-bottom-left-radius: 50%;
					}

					.num-edit-add {
						width: 55rpx;
						height: 55rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 1px solid #a5a5a5;
						border-top-right-radius: 50%;
						border-bottom-right-radius: 50%;
					}

					.num {
						width: 55rpx;
						height: 55rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 1px solid #a5a5a5;
					}
				}
			}

		}
	}
	.editColor {
		color: #cecece;
	}
</style>